package com.nari.ncps.screen.service.impl.ne;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.al.AlProdoperSales;
import com.nari.ncps.screen.mapper.ne.NeProdoperTimeRateMapper;
import com.nari.ncps.screen.service.ne.NeProdoperTimeRateService;
import com.nari.ncps.screen.vo.ne.NeTimeUtilizationRateVO;
import com.nari.ncps.screen.entity.ne.NeProdoperTimeRate;
import com.nari.ncps.screen.vo.wholeproduct.FuelConsumptionVO;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * 风电光电时间利用率表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class NeProdoperTimeRateServiceImpl extends ServiceImpl<NeProdoperTimeRateMapper, NeProdoperTimeRate> implements NeProdoperTimeRateService {

    @Override
    public List<NeTimeUtilizationRateVO> getTimeUtilizationRate(Integer year) {
        if (year == null) {
            year = java.time.LocalDate.now().getYear();
        }
        // 1. 确定查询的年份范围
        YearMonth currentYearMonth = YearMonth.now();
        String current = currentYearMonth.format(DateTimeFormatter.ofPattern("yyyyMM"));

        // 获取6个月之前的年月
        YearMonth twelveMonthsAgo = YearMonth.now().minusMonths(5);
        String startDate = twelveMonthsAgo.format(DateTimeFormatter.ofPattern("yyyyMM"));

        // 查询最近6个月的数据
        LambdaQueryWrapper<NeProdoperTimeRate> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(NeProdoperTimeRate::getOrgCode, 100360)
                .eq(NeProdoperTimeRate::getDateType,1)
                .ge(NeProdoperTimeRate::getStatisticTime, startDate)
                .le(NeProdoperTimeRate::getStatisticTime, current)
                .orderByDesc(NeProdoperTimeRate::getYear,NeProdoperTimeRate::getMonth);
        List<NeProdoperTimeRate> salesList1 = this.list(wrapper);
        List<NeTimeUtilizationRateVO> resultList = new ArrayList<>();
        for(NeProdoperTimeRate data : salesList1){
            NeTimeUtilizationRateVO vo = new NeTimeUtilizationRateVO();
            vo.setYear(data.getYear());
            vo.setMonth(data.getMonth());
            if (data.getTabWindFarm()!= null&& data.getTabWindFarm().compareTo(BigDecimal.ZERO) > 0){
                vo.setWindUtilizationRate(data.getTabWindFarm());
            }else {
                vo.setWindUtilizationRate(BigDecimal.ZERO);
            }
            if (data.getTbaSolarSite()!= null&& data.getTbaSolarSite().compareTo(BigDecimal.ZERO) > 0){
                vo.setSolarUtilizationRate(data.getTbaSolarSite());
            }else {
                vo.setSolarUtilizationRate(BigDecimal.ZERO);
            }
            resultList.add(vo);
        }
        resultList.sort(Comparator
                .comparingInt(NeTimeUtilizationRateVO::getYear)
                .thenComparingInt(NeTimeUtilizationRateVO::getMonth)
        );
        return resultList;
    }
}
